package com.bst.web.etl;

import com.bst.base.service.IBaseLabelService;
import com.bst.common.annotation.Log;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.enums.BusinessType;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.vo.HeaderAndBodyVO;
import com.bst.etl.domain.EtlDrawDw;
import com.bst.etl.service.IEtlDrawDwService;
import com.bst.etl.service.IEtlQryDwPubService;
import com.bst.common.vo.DwQueryParamVO;
import com.bst.md.domain.MdMedTbFld;
import com.bst.md.service.IMdMedTbFldService;
import com.bst.system.domain.BaseLabel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/base/etl/query/dw")
public class EtlDwPubQueryController extends BaseController {

    @Autowired
    private IBaseLabelService baseLabelService;
    @Autowired
    private IEtlDrawDwService etlDrawDwService;
    @Autowired
    private IMdMedTbFldService mdMedTbFldService;
    @Autowired
    private IEtlQryDwPubService etlQryDwPubService;

    @GetMapping("/self")
    public String exec(ModelMap mmap)
    {
        BaseLabel label = new BaseLabel();
        label.setParentId(0L);
        label.setSdCls("dw");
        List<BaseLabel> labels = baseLabelService.selectBaseLabelList(label);
        List<BaseLabel> children;
        for (BaseLabel l : labels) {
            label.setParentId(l.getIdLabel());
            children = baseLabelService.selectBaseLabelList(label);
            l.setChildren(children);
        }
        mmap.put("labels",labels);
        return "etl/query/dw/selfForDw";
    }

    @GetMapping("/toDetail")
    public String toDetail(Long idDw,ModelMap mmap) {
        EtlDrawDw dw = etlDrawDwService.selectEtlDrawDwByIdDw(idDw);
        MdMedTbFld tbFld = new MdMedTbFld();
        tbFld.setCdTb(dw.getCdTb());
        List<MdMedTbFld> tbFlds = mdMedTbFldService.selectMdMedTbFldList(tbFld);
        mmap.put("tbFlds",tbFlds);
        mmap.put("dw",dw);
        return "etl/query/dw/dw_detail";
    }

    @GetMapping("/showParam")
    public String showParam(Long idDw,String type,ModelMap mmap) {
        EtlDrawDw dw = etlDrawDwService.selectEtlDrawDwByIdDw(idDw);
        MdMedTbFld tbFld = new MdMedTbFld();
        tbFld.setCdTb(dw.getCdTb());
        List<MdMedTbFld> tbFlds = mdMedTbFldService.selectMdMedTbFldList(tbFld);

        mmap.put("tbFlds",tbFlds);
        mmap.put("dw",dw);
        mmap.put("type",type);
        mmap.put("defChartType","table");
        return "etl/query/dw/query_param";
    }

    @ApiOperation(value="指标综合查询-分页")
    @RequestMapping(value="/commonDwQuery", method= RequestMethod.POST)
    @ResponseBody
    public AjaxResult commonDwQuery(@RequestBody DwQueryParamVO paramVO) {
        AjaxResult result = etlQryDwPubService.findIndexDataWithHeader(paramVO);
        return result;
    }

    @Log(title = "指标综合查询导出", businessType = BusinessType.EXPORT)
    @PostMapping("/commonDwQuery/export")
    @ResponseBody
    public AjaxResult commonDwQueryExport(@RequestBody DwQueryParamVO paramVO)
    {
        paramVO.setPageSize(0);
        AjaxResult result = this.commonDwQuery(paramVO);
        HeaderAndBodyVO vo = (HeaderAndBodyVO)result.get(AjaxResult.DATA_TAG);
        String fileName = new ExcelUtil().createExcel(vo,paramVO.getFileName(),"Sheet");
        return AjaxResult.success(fileName);
    }
}
